//
//  WavOutput.m
//  CocoaSynth
//
//  Created by Simon André on 19/02/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WavOutput.h"
#import "SynthKit.h"
#import "System.h"


@implementation WavOutput

-(BOOL) recordSignal:(NSArray*) signal InFile:(NSString*) path{
	NSMutableData* audioData = [[NSMutableData alloc] init];
	// ---- RIFF Chunk Descriptor ---- //
	// ChunkId : RIFF
	NSString* riff = @"RIFF";
	[audioData appendData:[NSData dataWithBytes:[riff UTF8String] length:chunkIDBytesSize]];
	// ChunkSize : 36+signal size
	NSInteger size = ([signal count]*2)+headerSize;
	NSInteger* sizePointer = &size;
	[audioData appendBytes:sizePointer length:headerSizeBtesSize];
	// Format : WAVE
	NSString* wave = @"WAVE";
	[audioData appendData:[NSData dataWithBytes:[wave UTF8String] length:formatBytesSize]];
	// ---- fmt subchunk ---- //
	// Subchunk1ID : fmt
	NSString* fmt = @"fmt ";
	[audioData appendData:[NSData dataWithBytes:[fmt UTF8String] length:subchunkIdBytesSize]];
	// Subchunk1Size : 16 (PCM)
	NSInteger subChunkSize = subchunk1Size;
	NSInteger* subChunkSizePointer = &subChunkSize;
	[audioData appendBytes:subChunkSizePointer length:subchunk1SizeBytesSize];
	// AudioFormat : 1 (Uncompressed)
	NSInteger audioFormatInt = audioFormat;
	NSInteger* audioFormatPointer = &audioFormatInt;
	[audioData appendBytes:audioFormatPointer length:audioFormatSize];
	// NumChannels : 1
	NSInteger numChannelsInt = numChannels;
	NSInteger* numChannelsPointer = &numChannelsInt;
	[audioData appendBytes:numChannelsPointer length:numChannelsBytesSize];
	// SampleRate : frequency, obtained in System
	System* syst = [System sharedSystem];
	NSInteger sampleRate = [syst sampleRate];
	NSInteger* sampleRatePointer = &sampleRate;
	[audioData appendBytes:sampleRatePointer length:sampleRateBytesSise];
	// ByteRate : SampleRate * NumChannels * BitsPerSample/8
	NSInteger byteRate = sampleRate*numChannelsInt*(kBytesPerPacket/8);
	NSInteger* byteRatePointer = &byteRate;
	[audioData appendBytes:byteRatePointer length:byteRateSize];
	// BlockAlign : NumChannels * BitsPerSample/8
	NSInteger blockAlign = numChannelsInt*(kBytesPerPacket/8);
	NSInteger* blockAlignPointer = &blockAlign;
	[audioData appendBytes:blockAlignPointer length:blockAlignSize];
	// BitsPerSample : 4bits, 8bits ...
	NSInteger bitsPerSample = kBytesPerPacket;
	NSInteger* bitsPerSamplePointer = &bitsPerSample;
	[audioData appendBytes:bitsPerSamplePointer length:bitsPerSampleSize];
	// ---- data subchunk ---- //
	// Subchunk2ID : data
	NSString* dataSubChunk = @"data";
	[audioData appendData:[NSData dataWithBytes:[dataSubChunk UTF8String] length:subchunk2IdBytesSize]];					
	// Subchunk2IDSize : datas size
	NSInteger subChunk2IdSize = [signal count]*2;
	NSInteger* subChunk2IdSizePointer = &subChunk2IdSize;
	[audioData appendBytes:subChunk2IdSizePointer length:subchunk2SizeBytesSize];
	// data
	for (NSNumber* v in signal) {
		NSInteger val = ([v floatValue]*64);
		//NSLog(@"%f%@%d",[v floatValue],@"-",val);
		NSInteger* valPointer = &val;
		[audioData appendBytes:valPointer length:2];
		
		
		
		/*float aFloat = [v floatValue];
		unsigned char bytes[sizeof(float)];
		unsigned char *byte = (unsigned char *) &aFloat;
		for (int i=sizeof(float)-1; i>=0; i--)
		{
			bytes[i] = *byte;
			byte++;
		}
		unsigned char bytesRes[sizeof(float)];
		bytesRes[0] = bytes[2];
		bytesRes[1] = bytes[3];
		bytesRes[2] = bytes[0];
		bytesRes[3] = bytes[1];
		[audioData appendBytes:bytesRes length:4];*/
	}
	// writing
	if ([audioData writeToFile:path atomically:YES]) {
		return YES;
	}
	else {
		return NO;
	}
}

@end
